package com.study.spmongodb.book.wwj.char24;

import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @author MI
 * @version 1.0
 * @date 2020/11/30 21:40
 */
public class TaskHandler implements Runnable {

    //需要处理的请求
    private final Request request;

    public TaskHandler(Request request) {
        this.request = request;
    }

    @Override
    public void run() {

        System.out.println("Begin handle " + request);
        slowly();
        System.out.println("end handle " + request);
    }

    //模拟耗时请求，使线程进入短暂的休眠阶段
    private void slowly() {
        new Thread(() -> {

        }, "").start();

        try {
            TimeUnit.SECONDS.sleep(
                    ThreadLocalRandom.current().nextInt(10));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
